package com.zrt.springboot.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;

import com.zrt.springboot.common.Result;
import com.zrt.springboot.entity.User;
import com.zrt.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/echarts")
public class EchartsController {

    @Autowired
    private IUserService userService;

    @GetMapping("/example")
    public Result get() {
        System.out.println("======================example");
        Map<String, Object> map = new HashMap<>();
        map.put("x", CollUtil.newArrayList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));
        map.put("y", CollUtil.newArrayList(150, 230, 224, 218, 135, 147, 260));
        return Result.success(map);
    }

    @GetMapping("/members")
    public Result members() throws ParseException {
        List<User> list = userService.list();
        int q1 = 0; // 第一季度
        int q2 = 0; // 第二季度
        int q3 = 0; // 第三季度
        int q4 = 0; // 第四季度
        for (User user : list) {
            String createTime = user.getCreateTime();
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = ft.parse(createTime);
            Quarter quarter = DateUtil.quarterEnum(date);
            System.out.println(quarter);
            switch (quarter) {
                case Q1: q1 += 1; break;
                case Q2: q2 += 1; break;
                case Q3: q3 += 1; break;
                case Q4: q4 += 1; break;
                default: break;
            }
        }
        ArrayList<Integer> dataList = CollUtil.newArrayList(q1, q2, q3, q4);
        //System.out.println(dataList);
        return Result.success(dataList);
    }

    @GetMapping("/members2")
    public Result members2() throws ParseException {
        List<User> list = userService.list();
        int q1 = 0; // 第一季度
        int q2 = 0; // 第二季度
        int q3 = 0; // 第三季度
        int q4 = 0; // 第四季度
        for (User user : list) {
            String createTime = user.getCreateTime();
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = ft.parse(createTime);
            Quarter quarter = DateUtil.quarterEnum(date);
            switch (quarter) {
                case Q1: q1 += 1; break;
                case Q2: q2 += 1; break;
                case Q3: q3 += 1; break;
                case Q4: q4 += 1; break;
                default: break;
            }
        }
        ArrayList<Integer> dataList = CollUtil.newArrayList(q1, q2, q3, q4);
        Map<String, Object> map = new HashMap<>();
        map.put("name", CollUtil.newArrayList("第一季度", "第二季度", "第三季度", "第四季度"));
        map.put("values",  CollUtil.newArrayList(q1, q2, q3, q4));

        return Result.success(map);
    }

}
